package com.chengwei.ch.busdemo.view;

import android.util.Log;


import com.chengwei.ch.busdemo.hermes.contract.OnSendListener;
import com.chengwei.ch.busdemo.hermes.contract.OnSubscribeListener;
import com.chengwei.ch.busdemo.hermes.contract.OnUnSubscribeListener;

import org.jetbrains.annotations.NotNull;

import static com.chengwei.ch.busdemo.APP.mHermes;

/**
 * MQTT 收到数据
 */
public class MqttManager {
    private static MqttManager mMqttManager;
    private OnMessageArrivedListener mOnMessageArrivedListener;

    public static MqttManager getInstance() {
        if (mMqttManager == null) {
            synchronized (MqttManager.class) {
                if (mMqttManager == null) {
                    mMqttManager = new MqttManager();
                }
            }
        }
        return mMqttManager;
    }

    /**
     * 从MQTT上拉去数据
     */
    @SuppressWarnings("NullableProblems")
    public void getMQttDada(String subTopic) {
        mHermes.setStringSubTopic(subTopic);
        mHermes.setOnSubscribeListener(new OnSubscribeListener() {
            @Override
            public void onSubscribeSuccess(String topic) {
                Log.d("Tag", "订阅成功 : topic --->" + topic);
            }

            @Override
            public void onSubscribeFailure(String topic, Throwable cause) {
                Log.d("Tag", "订阅失败 : topic ---> " + topic + "    " + cause.getMessage());
            }

            @Override
            public void onMsgArrived(String subTopic, String msg) {
                Log.d("Tag", "数据接收 : subTopic ---> " + subTopic + "msg --->" + msg);
                if (mOnMessageArrivedListener != null) {
                    mOnMessageArrivedListener.onMsgArrived(msg);
                }
            }
        });
    }

    /**
     * 退订订阅
     */
    public void unsubscribe(String subTopic) {
        if (mHermes.isConnected() && subTopic != null) {
            mHermes.unsubscribe(subTopic);
            mHermes.setOnUnSubscribeListener(new OnUnSubscribeListener() {
                @Override
                public void unSubscribeSuccess(String topic) {
                    Log.d("Tag", "退订成功 : topic ---> $topic");
                }

                @Override
                public void unSubscribeFailure(String topic, Throwable cause) {
                    Log.d("Tag", "退订失败 : topic --->" + topic);
                }
            });
        }
    }

    /**
     * 发送消息队列到MQTT中
     */
    public void sendTopic(String subTopic, String content) {
        mHermes.sendTopic(subTopic, content);
        mHermes.setOnSendListener(new OnSendListener() {
            @Override
            public void onSendComplete(@NotNull String topic, @NotNull String content) {
                Log.d("Tag", "数据发送到队列失败topic --->" + topic);
            }

            @Override
            public void onSendFailure(@NotNull String topic, @NotNull Throwable cause) {
                Log.d("Tag", "数据发送到队列失败topic --->" + topic);
            }
        });
    }


    public void setOnMessageArrivedListener(OnMessageArrivedListener onMessageArrivedListener) {
        mOnMessageArrivedListener = onMessageArrivedListener;
    }

    /**
     * 数据接收
     */
    public interface OnMessageArrivedListener {
        void onMsgArrived(String msg);
    }
}